package com.syezon.wifikey.cengwang.viewmodel;

import android.arch.lifecycle.MutableLiveData;
import android.arch.lifecycle.ViewModel;


import com.syezon.wifikey.cengwang.WiFiTerminalUtils;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class TerminalViewModel extends ViewModel {

    private MutableLiveData<List<WiFiTerminalUtils.TerminalInfo>> terminalLiveData = new MutableLiveData<>();

    private MutableLiveData<WiFiTerminalUtils.TerminalInfo> terminalItemLiveData = new MutableLiveData<>();

    public MutableLiveData<List<WiFiTerminalUtils.TerminalInfo>> getTerminalLiveData() {
        return terminalLiveData;
    }

    public MutableLiveData<WiFiTerminalUtils.TerminalInfo> getTerminalItemLiveData() {
        return terminalItemLiveData;
    }

    public void getTerminalList() {
        Observable.create(new ObservableOnSubscribe<List<WiFiTerminalUtils.TerminalInfo>>() {
            @Override
            public void subscribe(ObservableEmitter<List<WiFiTerminalUtils.TerminalInfo>> e) throws Exception {
                List<WiFiTerminalUtils.TerminalInfo> terminalInWifi = WiFiTerminalUtils.getTerminalInWifi();
                WiFiTerminalUtils.TerminalInfo selfTerminalInfo = WiFiTerminalUtils.getSelfTerminalInfo();
                terminalInWifi.add(0, selfTerminalInfo);
                e.onNext(terminalInWifi);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<List<WiFiTerminalUtils.TerminalInfo>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<WiFiTerminalUtils.TerminalInfo> terminalInfos) {
                        terminalLiveData.postValue(terminalInfos);
                    }

                    @Override
                    public void onError(Throwable e) {
                        terminalLiveData.postValue(null);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void getTerminalItemList() {
        Observable.create(new ObservableOnSubscribe<WiFiTerminalUtils.TerminalInfo>() {
            @Override
            public void subscribe(ObservableEmitter<WiFiTerminalUtils.TerminalInfo> e) throws Exception {
                List<WiFiTerminalUtils.TerminalInfo> terminalInWifi = WiFiTerminalUtils.getTerminalInWifi();
                WiFiTerminalUtils.TerminalInfo selfTerminalInfo = WiFiTerminalUtils.getSelfTerminalInfo();
                terminalInWifi.add(0, selfTerminalInfo);
                for (int i = 0; i < terminalInWifi.size(); i++) {
                    e.onNext(terminalInWifi.get(i));
                }
                e.onNext(null);
            }
        }).subscribeOn(Schedulers.io())
                .subscribe(new Observer<WiFiTerminalUtils.TerminalInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WiFiTerminalUtils.TerminalInfo terminalInfos) {
                        terminalItemLiveData.postValue(terminalInfos);
                    }

                    @Override
                    public void onError(Throwable e) {
                        terminalItemLiveData.postValue(null);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
